package com.chinawayltd.ews.sample.util;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;

/**
 * @program: greeProject
 * @description:
 * @author: xiazj
 * @create: 2018-04-23 12:21
 **/
public class BaseController {

    protected void sendWorkbook(Workbook workbook, String fileName, HttpServletRequest request, HttpServletResponse response) throws IOException {
        String templetName = new String(fileName);
        response.reset();
        String agent = request.getHeader("user-agent");
        if(null != agent && -1 != agent.indexOf("MSIE")){//IE
            templetName = java.net.URLEncoder.encode(fileName,"UTF-8");
        }else if(null != agent && -1 != agent.indexOf("Mozilla")){
            templetName = new String (fileName.getBytes("GBK"),"iso-8859-1");
        }else{
            templetName = java.net.URLEncoder.encode(fileName,"UTF-8");
        }
        response.setHeader("Content-disposition", "attachment;filename=" + templetName + ".xls");
        response.setContentType("application/octet-stream");
        response.setContentType("application/OCTET-STREAM;charset=UTF-8");
        response.setHeader("Access-Control-Allow-Origin" , "*");
        workbook.write(response.getOutputStream());
    }

}
